package kr.co.pension.schedule;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import kr.co.pension.usr.pe.re.service.PEREService;
import kr.co.pension.usr.pe.re.vo.PEREVo;
import kr.co.pension.utils.sms.SMSUtil;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 예약 자동 삭제 프로세스
 * @author devhw
 *
 */
public class DeleteReservationProcessor {

	@Autowired
	private PEREService service;

	Logger log = Logger.getLogger(getClass());
	
	private static boolean chk = false;
	 /**
	 * @throws Exception 
	 * 
	 */
//	@Transactional(propagation=Propagation.REQUIRED)
//	@Scheduled(cron="10 * * * * *")
	public void process() throws Exception{
		SMSUtil smsUtil = new SMSUtil();
		
		//두번 실행되는거 방지 처리
//		if(getChk()){

			PEREVo vo = new PEREVo();
			
			//지금시간 설정
			SimpleDateFormat frmDate = new SimpleDateFormat("yyyyMMddHHmm00");
			Calendar d1 = Calendar.getInstance();//오늘
			vo.setNow_date(frmDate.format(d1.getTime()));
			
			List pList = service.pere600_1Q(vo);
			
			if(null!=pList && 0<pList.size()){
				for(int i =0; i< pList.size();i++){
					Map data = (Map) pList.get(i);
					vo.setWait_time((Integer) data.get("wait_time"));
					vo.setP_id((String)data.get("p_id"));
					//기간이 지난 자료들 찾기
					List rList = service.pere600_2Q(vo);

					String nameChk = "";
					if(null!=rList && 0<rList.size()){
						for(int ii =0; ii< rList.size();ii++){
							Map map = (Map) rList.get(ii);
							
							if(!nameChk.equals(map.get("reservation_nm"))){
								

								if("Y".equals( data.get("e1") ) || "Y".equals( data.get("l1") )){
									//사용자 sms 보내기
									
//									예약자	[펜션명]미입금으로 예약이 취소되었습니다		/*
//									관리자	[미입급예약삭제]2009-00-00/객실명/1박/아무개*/
									
									
									StringBuffer msg = new StringBuffer();
									String send = data.get("p_tel1")+"-"+data.get("p_tel2")+"-"+data.get("p_tel3");
									String recive = map.get("r_phone1")+"-"+map.get("r_phone2")+"-"+map.get("r_phone3");
									
									msg.append("[");
									msg.append(data.get("p_nm"));
									msg.append("] ");
									msg.append(" 미입금으로 예약이 취소되었습니다");

									String returnCode = smsUtil.send(send, recive, msg.toString());
									
									//sms 히스토리에 넣기
									vo.setP_nm((String) data.get("p_nm") );
									vo.setFrom_no(send);
									vo.setTo_no(recive);
									vo.setSms_content(msg.toString());
									vo.setC_code(returnCode);
									vo.setC_msg(smsUtil.smsReturnMsg(vo.getC_code()));
									
									service.pere710Q(vo);
									
									if("1".equals(returnCode)){
										//sms 카운트 줄이기
										service.pere711Q(vo);
									}
									//sms 히스토리에 넣기 종료
								}
								if("Y".equals( data.get("e2") ) || "Y".equals( data.get("l2") )){
									//관리자 sms보내기

									StringBuffer msg = new StringBuffer();
									String send = map.get("r_phone1")+"-"+map.get("r_phone2")+"-"+map.get("r_phone3");
									String recive = data.get("p_phone1")+"-"+data.get("p_phone2")+"-"+data.get("p_phone3");
									
									msg.append("[미입급예약삭제] ");
									msg.append(map.get("reservation_dt_n"));
									
									msg.append("/");
									msg.append(map.get("room_nm"));
									msg.append("/");
									msg.append(map.get("reservation_nm"));

									String returnCode = smsUtil.send(send, recive, msg.toString());
									//sms 히스토리에 넣기
									vo.setP_nm((String) data.get("p_nm") );
									vo.setFrom_no(send);
									vo.setTo_no(recive);
									vo.setSms_content(msg.toString());
									vo.setC_code(returnCode);
									vo.setC_msg(smsUtil.smsReturnMsg(vo.getC_code()));
									
									service.pere710Q(vo);
									if("1".equals(returnCode)){
										//sms 카운트 줄이기
										service.pere711Q(vo);
									}

									//sms 히스토리에 넣기 종료
								}
								
								nameChk = (String)map.get("reservation_nm");
							}
							
							vo.setReservation_dt((String) map.get("reservation_dt"));
							vo.setPr_no((Integer)map.get("pr_no"));
							service.pere600_1T(vo);
							service.pere600_2T(vo);
						}
					}
				}
			}
			
			//삭제 30분전 자료 찾기
			List smsList30 = service.pere600_3Q(vo);
			if(null!=smsList30 && 0<smsList30.size()){
				for(int ii =0; ii< smsList30.size();ii++){
					Map map = (Map) smsList30.get(ii);
					
					StringBuffer msg = new StringBuffer();
					int totalPay=0;
					
					String r_info =(String) map.get("r_info");
					
					//부가서비스 가격
					String add_svc = r_info.split("@")[0];
					String into = r_info.split("@")[1];
					int pay = 0;
					for(int j=0; j<into.split(";").length;j++){
						String[] vals = into.split(";")[j].split(":");
						pay +=Integer.parseInt(vals[6]);
					}
					
					totalPay=(pay+Integer.parseInt(add_svc));
					
//					[펜션명]객실명-국민은행 142313-123-123132 100,000원
//					30분후 예약이 자동삭제됩니다
					java.text.DecimalFormat decFormat = new java.text.DecimalFormat("###,###,###,###");
					
					msg.append("[");
					msg.append(map.get("p_nm"));
					msg.append("] ");
					msg.append(map.get("room_nm"));
					msg.append("-");
					msg.append(map.get("send_bank"));
					msg.append(" ");
					msg.append(decFormat.format(totalPay));
					msg.append("원");
					msg.append("30분후 예약이 자동취소됩니다");
					
					if("Y".equals( map.get("d1")) || "Y".equals(map.get("k1")) ){
						//사용자 sms 전송
						String send = map.get("p_tel1")+""+map.get("p_tel2")+map.get("p_tel3");
						String recive = map.get("r_phone1")+""+map.get("r_phone2")+map.get("r_phone3");

						String returnCode = smsUtil.send(send, recive, msg.toString());
						//sms 히스토리에 넣기
						vo.setP_nm((String) map.get("p_nm") );
						vo.setFrom_no(send);
						vo.setTo_no(recive);
						vo.setSms_content(msg.toString());
						vo.setC_code(returnCode);
						vo.setC_msg(smsUtil.smsReturnMsg(vo.getC_code()));
						
						service.pere710Q(vo);
						if("1".equals(returnCode)){
							//sms 카운트 줄이기
							service.pere711Q(vo);
						}
					}
					if("Y".equals( map.get("d2")) || "Y".equals("k2") ){
						//관리자 sms 전송
						String send = map.get("r_phone1")+""+map.get("r_phone2")+map.get("r_phone3");
						String recive = map.get("p_phone1")+""+map.get("p_phone2")+map.get("p_phone3");


						String returnCode = smsUtil.send(send, recive, msg.toString());
						//sms 히스토리에 넣기
						vo.setP_nm((String) map.get("p_nm") );
						vo.setFrom_no(send);
						vo.setTo_no(recive);
						vo.setSms_content(msg.toString());
						vo.setC_code(returnCode);
						vo.setC_msg(smsUtil.smsReturnMsg(vo.getC_code()));
						
						service.pere710Q(vo);
						if("1".equals(returnCode)){
							//sms 카운트 줄이기
							service.pere711Q(vo);
						}
					}
				}
			}
//			setChk(false);
			
//		}else{
//			chk=true;
//			setChk(true);
//		}
	}
	
	public static void setChk(boolean chk) {
		DeleteReservationProcessor.chk = chk;
	}

	public synchronized boolean getChk(){
		return chk;
	}
	 
}
